package com.haoran.dwz.common.security;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.UrlPathHelper;

import com.haoran.dwz.service.SystemSetupService;

@Component
public class UsmInitializationFilter extends OncePerRequestFilter {
	
	private static final String USM_CONTEXT_FULLNAME = "USM_CONTEXT_FULLNAME";

	private final UrlPathHelper pathHelper = new UrlPathHelper();
	
	@Autowired
	private SystemSetupService systemSetupService;
	

	@Value("${usm.verificationCode.enabled:true}")
	private boolean verificationCode;
	
	@Value("${usm.verificationCode.failure:3}")
	private int loginFailureTimes;
	
	@Value("${usm.title.fullname:统一系统管理}")
	private String fullname;
	@Value("${usm.title.shortname:USM}")
	private String shortname;
	
	/**
	 * 是否显示验证码
	 */
	public static final  String SESSION_USM_VERIFICATIONCODE_ENABLED_KEY = "USM_VERIFICATIONCODE_ENABLED";
	
	/**
	 * 失败多少次就显示验证码
	 */
	public static final  String SESSION_USM_LOGIN_FAILURE_TIMES_KEY = "USM_LOGIN_FAILURE_TIMES";

	
	private static final String USM_CONTEXT_SHORTNAME = "USM_CONTEXT_SHORTNAME";

	public boolean matches(HttpServletRequest request) {
		String lookupPath = this.pathHelper.getLookupPathForRequest(request);
		return matches(lookupPath);
	}

	private boolean matches(String lookupPath) {
		return "/install".equals(lookupPath) || "/init".equals(lookupPath);
	}

	protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
		String lookupPath = this.pathHelper.getLookupPathForRequest(request);
		return   lookupPath.startsWith("/statics") || lookupPath.startsWith("/static") ||  lookupPath.endsWith(".map") || lookupPath.endsWith(".ico") || lookupPath.endsWith(".js") || lookupPath.endsWith(".css") || lookupPath.endsWith(".woff2")|| lookupPath.endsWith(".ttf") || lookupPath.endsWith(".woff");
	}

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		String lookupPath = this.pathHelper.getLookupPathForRequest(request);
		request.getServletContext().setAttribute(USM_CONTEXT_FULLNAME, fullname);
		request.getServletContext().setAttribute(USM_CONTEXT_SHORTNAME, shortname);
		if (!systemSetupService.isSetup() && !matches(request)) {
			response.sendRedirect("install");
		} else if (StringUtils.equals(lookupPath, "/login")){
			Object failtureTimes = request.getSession().getAttribute(SESSION_USM_LOGIN_FAILURE_TIMES_KEY);
			Integer times = 0;
			if(null != failtureTimes) {
				times = Integer.valueOf(failtureTimes.toString());
			}
			request.getSession().setAttribute(SESSION_USM_VERIFICATIONCODE_ENABLED_KEY, verificationCode && times > loginFailureTimes);
			filterChain.doFilter(request, response);
		}else {
			filterChain.doFilter(request, response);
		}
	}

	public boolean isVerificationCode() {
		return verificationCode;
	}

	public void setVerificationCode(boolean verificationCode) {
		this.verificationCode = verificationCode;
	}
	
}
